/**
 * 
 */
package io.dora.rpc;

import java.util.concurrent.ConcurrentHashMap;

/**
 * RPC 响应结果默认实现
 * 
 * @author zhaohongbo
 *
 */
public class RpcFutureResult implements FutureResult {
	
	public static ConcurrentHashMap<Long, Object> FUTURE_RESULT = new ConcurrentHashMap<Long, Object>();

	/** 请求ID */
	private long id;

	/** 默认超时时长3秒 */
	public static final int DEFAULT_TIMEOUT = 3000;
	
	public RpcFutureResult(long id) {
		this.id = id;
	}

	@Override
	public Object get() {
		Object result = null;
		long start = System.currentTimeMillis();
		while(result == null && !isTimeout(start)) {
			result = FUTURE_RESULT.get(id);
		}
		FUTURE_RESULT.remove(id);
		return result;
	}
	
	public static boolean isTimeout(long start) {
		return (System.currentTimeMillis() - start) > DEFAULT_TIMEOUT;
	}
}
